﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;

namespace Gundam_2._0_Alpha.src
{
    public class Gravity: CharDrawAction
    {
        private Sprite gravitySprite = Sprites.CreateSprite(Resources.LoadBitmap("gravity.png"), 6,2, 432, 389);
        private Point2D gravityLoc;
        private bool isPlayer;
        private int duration;
        private Timer timer = Core.CreateTimer();

        public Gravity(Character c, bool persistant, int row, int colEnd, int count, int lockoutLevel, int minDamage, bool isPlayer, int duration)
            : base(c, persistant, row, colEnd, count, lockoutLevel)
        {
            this.duration = duration;
            this.isPlayer = isPlayer;
            gravitySprite.X = GlobalConst.ScreenWidth / 2 - gravitySprite.Width/2;
            gravitySprite.Y = GlobalConst.ScreenHeight / 2 - gravitySprite.Height/2;
            gravityLoc.X = gravitySprite.X + gravitySprite.Width / 2;
            gravityLoc.Y = gravitySprite.Y + gravitySprite.Height / 2;
        }

        public override void DoAction(GameController gc)
        {
            if (!IsActive && Core.TimerTicks(timer) == 0)
            {
                IsActive = true;
                Core.StartTimer(timer);
            }
            else if (!IsActive && Core.TimerTicks(timer) > duration)
                Reset(gc);

            if (IsActive)
            {
                foreach (DestroyableObject obj in gc.GameStages.Peek().CharList)
                    if (obj is Character && (obj is Player) == isPlayer)
                    {
                        Vector v = Sprites.VectorFromCenterSpriteToPoint(obj.ObjectSprite, gravityLoc);
                        v = Geometry.VectorMultiply(Geometry.UnitVector(v), 2.0f);

                        obj.ObjectSprite.X += v.x;
                        obj.ObjectSprite.Y += v.y;
                    }
                GlobalConst.DmgCheck.CheckDamage(gc.GameStages.Peek().CharList, gravitySprite, isPlayer, 300, gc, false, 0);
            }
        }

        public override void Draw()
        {
            gravitySprite.Draw();
            gravitySprite.UpdateAnimation();
        }

        public override void Reset(GameController gc)
        {
            IsActive = false;
            Core.StopTimer(timer);
        }
    }
}
